﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class MasterCommitteeFunctionDAL : IDALMasterCommitteeFunction
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasterCommitteeFunctionDAL()
        {

        }

        public IList<MasterCommitteeFunctionInfo> GetMasterCommitteeFunction()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeFunctionInfo> infoList = new List<MasterCommitteeFunctionInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_COMMITTEE_FUNCTION"
                        + " WHERE RECORD_STATUS <> @delete_status"
                        + " ORDER BY icm_com_func_seq ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeFunctionInfo info = new MasterCommitteeFunctionInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_FUNC_SEQ"]);
                        info.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        info.Descr = (dr[i]["ICM_COM_FUNC_DESCR"]).ToString();
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus; 
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterCommitteeFunctionInfo> GetMasterCommitteeFunction(string record_status)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeFunctionInfo> infoList = new List<MasterCommitteeFunctionInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@record_status", record_status);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_COMMITTEE_FUNCTION"
                        + " WHERE RECORD_STATUS = @record_status"
                        + " ORDER BY icm_com_func_seq ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeFunctionInfo info = new MasterCommitteeFunctionInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_FUNC_SEQ"]);
                        info.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        info.Descr = (dr[i]["ICM_COM_FUNC_DESCR"]).ToString();
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterCommitteeFunctionInfo> GetMasterCommitteeFunctionWithMapping(int map_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterCommitteeFunctionInfo> infoList = new List<MasterCommitteeFunctionInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@map_seq", map_seq);

                strSQL = "SELECT DISTINCT f.ICM_COM_FUNC_SEQ"
                            + ", f.ICM_COM_FUNC_NAME"
                        + " FROM ICM_MAS_COMMITTEE_MAPPING m"
                            + " INNER JOIN ICM_MAS_COMMITTEE_FUNCTION f ON m.ICM_COM_FUNC_SEQ = f.ICM_COM_FUNC_SEQ"
                        + " WHERE f.RECORD_STATUS <> @delete_status"
                             + " AND m.RECORD_STATUS <> @delete_status"
                             + " AND m.ICM_PC_MAP_SEQ = @map_seq"
                             + " ORDER BY icm_com_func_seq ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterCommitteeFunctionInfo info = new MasterCommitteeFunctionInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_COM_FUNC_SEQ"]);
                        info.Name = (dr[i]["ICM_COM_FUNC_NAME"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public MasterCommitteeFunctionInfo GetMasterCommitteeFunction(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            MasterCommitteeFunctionInfo info = new MasterCommitteeFunctionInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_COMMITTEE_FUNCTION"
                        + " WHERE ICM_COM_FUNC_SEQ = @seq"
                        + " ORDER BY icm_com_func_seq ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    info.Seq = Convert.ToInt32(dr[0]["ICM_COM_FUNC_SEQ"]);
                    info.Name =  (dr[0]["ICM_COM_FUNC_NAME"]).ToString();
                    info.Descr = (dr[0]["ICM_COM_FUNC_DESCR"]).ToString();
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoRecord.Restored_by = (Convert.IsDBNull(dr[0]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[0]["RESTORED_BY"]));
                    infoRecord.Restored_when = (Convert.IsDBNull(dr[0]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["RESTORED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(MasterCommitteeFunctionInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@name", info.Name);
                dbManager.AddParameters(1, "@descr", info.Descr);
                dbManager.AddParameters(2, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(3, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(4, "@created_when", info.InfoRecord.Created_when);

                strSQL = "INSERT INTO ICM_MAS_COMMITTEE_FUNCTION"
                    + " (ICM_COM_FUNC_NAME, ICM_COM_FUNC_DESCR"
                    + " , RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@name"
                    + ", @descr"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasterCommitteeFunctionInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@name", info.Name);
                dbManager.AddParameters(2, "@descr", info.Descr);
                dbManager.AddParameters(3, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(4, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(5, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_FUNCTION"
                    + " SET ICM_COM_FUNC_NAME = @name"
                    + ", ICM_COM_FUNC_DESCR = @descr"
                    + ", RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_FUNC_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasterCommitteeFunctionInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(3, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_FUNCTION"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_FUNC_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<MasterCommitteeFunctionInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", infoList[0].InfoRecord.Updated_by);
                dbManager.AddParameters(2, "@updated_when", infoList[0].InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_FUNCTION"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_COM_FUNC_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<MasterCommitteeFunctionInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_MAS_COMMITTEE_FUNCTION"
                    + " WHERE ICM_COM_FUNC_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Restore(IList<MasterCommitteeFunctionInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@restored_by", infoList[0].InfoRecord.Restored_by);
                dbManager.AddParameters(2, "@restored_when", infoList[0].InfoRecord.Restored_when);

                strSQL = "UPDATE ICM_MAS_COMMITTEE_FUNCTION"
                    + " SET RECORD_STATUS = @record_status"
                    + ", RESTORED_BY = @restored_by"
                    + ", RESTORED_WHEN = @restored_when"
                    + " WHERE ICM_COM_FUNC_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
